package huoye.shops.huoye.mine.activity;

import android.net.Uri;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.codeideology.android.previewimage.PrevieImage;
import com.codeideology.android.previewimage.PrevieImageActivity;
import com.codeideology.android.utils.BarUtils;
import com.codeideology.android.utils.StringUtils;
import com.codeideology.android.widget.recycler.RecyclerViewAdapter;
import com.jph.takephoto.model.TImage;
import com.jph.takephoto.model.TResult;
import com.just.agentweb.AgentWeb;
import com.just.agentweb.AgentWebView;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import huoye.shops.R;
import huoye.shops.base.APIUrls;
import huoye.shops.base.BaseActivity;
import huoye.shops.base.BaseTakePhotoActivity;
import huoye.shops.base.MyWebView;
import huoye.shops.base.UserManager;
import huoye.shops.common.AddPictureAdapter;
import huoye.shops.common.model.OssImageData;
import huoye.shops.huoye.mine.adapter.EnterPactAdapter;
import huoye.shops.huoye.mine.model.ContractData;
import huoye.shops.huoye.mine.model.EnterPactData;
import huoye.shops.huoye.mine.presenter.EnterPactPresenter;
import huoye.shops.huoye.mine.view.IEnterPactView;
import huoye.shops.huoye.operation.activity.EditAddShopActivity;
import huoye.shops.huoye.operation.activity.LimitDiscountActivity;
import huoye.shops.huoye.operation.model.AddImage;
import huoye.shops.huoye.operation.model.DetialImgs;
import huoye.shops.huoye.operation.widget.HintTitleDialog;
import huoye.shops.utils.StockUtils;

public class EnterPactActivity extends BaseTakePhotoActivity implements IEnterPactView {

    @BindView(R.id.enter_pact_wb)
    LinearLayout  mAgentWeb;


    /*@BindView(R.id.enter_pact_sv)
    ScrollView sv;*/


    @BindView(R.id.enter_pact_iv_add)
    ImageView ivAddPic;


    private EnterPactPresenter mEnterPactPresenter;

    @OnClick(R.id.enter_pact_iv_add)
    void onAddPicClick(){
        if(isEnterPactYes){
            //makeToast("我是编辑");
        }else {
            //  makeToast("我是新增");
            takePhoto();
        }
    }







    @BindView(R.id.enter_pact_rv)
    RecyclerView recyclerView;


    private List<String> contractImgs = new ArrayList<>();

    @BindView(R.id.enter_pact_btn_add)
    Button btnAddEnterPact;

    @OnClick(R.id.enter_pact_btn_add)
    void onEnterPactClick(){
       showEnterPactDialog();
    }

    private void setEnterPact(){
        List<AddImage> images = adapter.getData();
        if (adapter != null || images != null) {
            for (AddImage image : images) {
                if (StringUtils.isNotEmpty(image.getImageUrl())) {
                    contractImgs.add(image.getImageUrl());

                }
            }
        }

        if(contractImgs.size() == 0){
            makeToast("入驻合同相册不能为空");
            return;
        }
        mEnterPactPresenter.SetContract(contractImgs);
    }


    private void showEnterPactDialog(){
        HintTitleDialog hintTitleDialog = new HintTitleDialog(this);
        hintTitleDialog.setOnHintTitleListener(new HintTitleDialog.OnHintTitleListener() {
            @Override
            public void onLeftClick() {

            }

            @Override
            public void onRightClick() {
                setEnterPact();
            }
        });
        hintTitleDialog.show();
        hintTitleDialog.setDialogContent("补全合同","合同补全就不能进行修改","取消","确定");
    }



    boolean isEnterPactYes = false;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_enter_pact;
    }

    @Override
    protected void initView(@Nullable Bundle bundle) {
        setTitle("入驻合同");
        mEnterPactPresenter = new EnterPactPresenter();
        mEnterPactPresenter.addTaskListener(this);
        mEnterPactPresenter.onStart();
        mEnterPactPresenter.contractInfo();

    }



    private String imgPath = "";

    @Override
    public void takeSuccess(TResult result) {
        if (result != null) {

            TImage image = result.getImage();
            String userShopId = UserManager.getInstance().getUserShopId();
            AddImage img = new AddImage();
            //上传单张图片
            imgPath = image.getCompressPath();
            mEnterPactPresenter.uploadOssImg(userShopId,"shop",imgPath);
            img.setPath(imgPath);
            img.setLocal(true);
            if (adapter.getListCount() == 0) {
                adapter.add(0, img);
            } else {
                adapter.add(adapter.getListCount(), img);
            }
            adapter.notifyDataSetChanged();
        }
    }


    private List<ContractData>  mContractData;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void getContractData(EnterPactData enterPactData) {
        this.mContractData = enterPactData.getContractImgs();
        setWebView(enterPactData.getAgreeMentUrl());
        if (mContractData.size() > 0) {
            showEnterPactAdapter();
            btnAddEnterPact.setVisibility(View.GONE);
        }else {
            showAdapter();
        }


    }

    private void showEnterPactAdapter(){
        EnterPactAdapter enterPactAdapter = new EnterPactAdapter(this);
        enterPactAdapter.setData(mContractData);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        recyclerView.setAdapter(enterPactAdapter);
    }





    private void setWebView(String url){
       AgentWeb.with(this)
                .setAgentWebParent(mAgentWeb, new ScrollView.LayoutParams(-1, -1))
                .useDefaultIndicator()
                .createAgentWeb()
                .ready()
                .go(url);
    }



    private int max = 3;
    private AddPictureAdapter adapter;
    private List<AddImage> AddImageList = new ArrayList<>();

    private void showAdapter() {
        adapter = new AddPictureAdapter(this);
        if(mContractData != null) {
            if (mContractData.size() > 0) {
                for (int i = 0; i < mContractData.size(); i++) {
                    AddImage addImage = new AddImage();
                    addImage.setImageUrl(mContractData.get(i).getImgUrl());
                    addImage.setLocal(false);
                    AddImageList.add(addImage);
                }

                adapter.setData(AddImageList);
            }
        }
            adapter.setMaxCount(max);
            adapter.setOnItemClickListener(new AddPictureAdapter.OnItemClickListener<AddImage>() {
                @Override
                public void onItemClick(AddImage addImage, int position, View view) {
                    int count = adapter.getItemCount();
                    if (adapter.getListCount() == max) {
                        if (position == max - 1) {
                            return;
                        }
                    } else {
                        if (position == count - 1) {
                            takePhoto();
                            return;
                        }
                    }
                    List<PrevieImage> images = new ArrayList<>();
                    List<AddImage> data = adapter.getData();
                    for (int i = 0; i < data.size(); i++) {
                        PrevieImage image = new PrevieImage();
                        String url = APIUrls.COMMON_CONFIG.TYPE_COMMON_ALIYUN + data.get(i).getImageUrl();
                        image.setThumbnailUrl(url);
                        image.setImageViewWidth(view.getWidth());
                        image.setImageViewHeight(view.getHeight());
                        int[] points = new int[2];
                        view.getLocationInWindow(points);
                        image.setImageViewX(points[0]);
                        image.setImageViewY(points[1] - BarUtils.getStatusBarHeight(EnterPactActivity.this));
                        images.add(image);
                    }
                    PrevieImageActivity.onStartActivity(EnterPactActivity.this, (ArrayList<PrevieImage>) images, position);
                }
            });
            recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
            recyclerView.setAdapter(adapter);
    }





    @Override
    public void setUpload(OssImageData ossImageData) {
        List<AddImage> images = adapter.getData();
        if (adapter == null || images == null || images.size() < 1) {
            return;
        }
        for (AddImage image : images) {
            if (image.getPath().equals(imgPath)) {
                image.setImageUrl(ossImageData.getFilePath());
            }
        }

     /*   imgPath = ossImageData.getFilePath();
        List<String> list = new ArrayList<>();
        list.add(imgPath);
        mEnterPactPresenter.SetContract(list);*/
    }

    @Override
    public void setDelete() {
    /*    ivAddPic.setImageResource(R.mipmap.ic_add_image);
        ivDelPic.setVisibility(View.GONE);*/
    }
}
